package cn.jsony.pet.controller;

import cn.jsony.basic.config.BusinessException;
import cn.jsony.pet.service.ISearchMasterMsgService;
import cn.jsony.pet.domain.SearchMasterMsg;
import cn.jsony.pet.query.SearchMasterMsgQuery;
import cn.jsony.basic.util.AjaxResult;
import cn.jsony.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }


    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request) {
        try {
            searchMasterMsgService.publish(msg,request);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("发布失败"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙");
        }
    }

    /*用户查询寻主消息*/
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userQuery(@RequestBody SearchMasterMsgQuery query,HttpServletRequest requestequest) {
        return searchMasterMsgService.userQuery(query,requestequest);
    }

    /*后台管理员查询状态为0(待审核)的寻主消息*/
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0L);
        return searchMasterMsgService.queryPage(query);
    }

    /*后台管理员查询状态为2(审核完成)的寻主消息*/
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        query.setState(2L);
        return searchMasterMsgService.queryPage(query);
    }


    /*后台管理员查询状态为1(审核成功待处理)的寻主消息*/
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        query.setState(1L);
        return searchMasterMsgService.toHandle(query,request);
    }

    /*拒单*/
    @GetMapping(value="/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){//寻主消息id
        try {
            searchMasterMsgService.reject(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"拒单失败");
        }
    }

    // 接单
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody Map<String,Object> params){
        try {
            searchMasterMsgService.accept(params);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("接单失败！"+e.getMessage());
        }
    }
}
